<?php
/**
  * 签名生成算法
  * @param  array  $params API调用的请求参数集合的关联数组，不包含sign参数
  * @param  string $secret 签名的密钥即获取access token时返回的session secret
  * @return string 返回参数签名值
  */
 function getSignature($params, $secret = '123456')
 {
    $str = '';  //待签名字符串
    //先将参数以其参数名的字典序升序进行排序
    ksort($params);
    //遍历排序后的参数数组中的每一个key/value对
    foreach ($params as $k => $v) {
        //为key/value对生成一个key=value格式的字符串，并拼接到待签名字符串后面
        $str .= "$k=$v";
    }
    //将签名密钥拼接到签名字符串最后面
    $str .= $secret;
    //通过md5算法为签名字符串生成一个md5签名，该签名就是我们要追加的sign参数值
    return md5($str);
 }
 
 /**
 * 加密
 *
 * @param string $str 被加密的字符串
 * @return string
 */
 
function encrypt($str)
{
    $str = base64_encode($str);
    for ($i = 0; $i < 3; $i++) {
        $str = strrev(substr($str, 7) . substr($str, 0, 7));
    }
    return $str;
}

/**
 * 解密
 *
 * @param string $str 被解密的字符串
 * @return string
 */
function decrypt($str)
{
    for ($i = 0; $i < 3; $i++) {
        $str = strrev($str);
        $str = substr($str, -7) . substr($str, 0, -7);
    }
    $str = base64_decode($str);
    return $str;
}


/**
 * 对称加解密KEY
 * @var array
 * @see wc*0&}yTUP!.~;Edg<{CB-2u@DNsb6[kYvi)QA$_Ljn%3O8"=95ao:GXMI(SzRfxq#Km1h`ltFrW^,Z+?|JH/>V]p47e'\
 */


/**
 * 对称加解密
 * @param string $str
 * @return string
 */
function edstr($str)
{
     $key = array('w' => '"', 'c' => '=', '*' => '9', '0' => '5', '&' => 'a', '}' => 'o', 'y' => ':', 'T' => 'G', 'U' => 'X', 'P' => 'M', '!' => 'I', '.' => '(', '~' => 'S', ';' => 'z', 'E' => 'R', 'd' => 'f', 'g' => 'x', '<' => 'q', '{' => '#', 'C' => 'K', 'B' => 'm', '-' => '1', '2' => 'h', 'u' => '`', '@' => 'l', 'D' => 't', 'N' => 'F', 's' => 'r', 'b' => 'W', '6' => '^', '[' => ',', 'k' => 'Z', 'Y' => '+', 'v' => '?', 'i' => '|', ')' => 'J', 'Q' => 'H', 'A' => '/', '$' => '>', '_' => 'V', 'L' => ']', 'j' => 'p', 'n' => '4', '%' => '7', '3' => 'e', 'O' => '\'', '8' => '\\', '"' => 'w', '=' => 'c', '9' => '*', '5' => '0', 'a' => '&', 'o' => '}', ':' => 'y', 'G' => 'T', 'X' => 'U', 'M' => 'P', 'I' => '!', '(' => '.', 'S' => '~', 'z' => ';', 'R' => 'E', 'f' => 'd', 'x' => 'g', 'q' => '<', '#' => '{', 'K' => 'C', 'm' => 'B', '1' => '-', 'h' => '2', '`' => 'u', 'l' => '@', 't' => 'D', 'F' => 'N', 'r' => 's', 'W' => 'b', '^' => '6', ',' => '[', 'Z' => 'k', '+' => 'Y', '?' => 'v', '|' => 'i', 'J' => ')', 'H' => 'Q', '/' => 'A', '>' => '$', 'V' => '_', ']' => 'L', 'p' => 'j', '4' => 'n', '7' => '%', 'e' => '3', '\'' => 'O', '\\' => '8',);
    return strtr($str, $key);
}